#include "ui_flashwidget.h"
#include "ui_manager.h"
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	#include "flash/flash_movie.h"
#endif


#include "audio/audio.h"

#include "util/timer.h"		    // Needs to come in early
#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"		    // logging

#include "script/scripthelper.h"
#include "script/write_lua.h"

using namespace ui;
using namespace script;

#ifdef _ENABLE_LUABIND
#include <luabind/luabind.hpp>
#endif // _ENABLE_LUABIND

UIFlashWidget::UIFlashWidget(UIWidget* pParent,const std::string& Name) :
	UIWidget(pParent, Name)
// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	,m_pFlashMovie(NULL)
#endif
	
{
	m_Type = WT_FLASHWIDGET;

	// ensure that flash is initialized
	m_pManager->EnableFlash();
	m_pManager->InitFlash();

}

UIFlashWidget::~UIFlashWidget()
{
// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	UTIL_SAFE_DELETE(m_pFlashMovie);
#endif
	
}

bool		UIFlashWidget::Create(const std::string& Name, const math::Vec2& Pos, const math::Vec2& Size)
{

	UIWidget::Create(Name, Pos, Size);
	// create progress stuff

	return true;
}

bool		UIFlashWidget::Load(LuaPlus::LuaObject& _LObj)
{
	UIWidget::Load(_LObj);	// get all the common stuff loaded
	std::string FlashMovie = ScriptHelper::GetTableString(_LObj, "FlashMovie", "");
	bool		Looping		= ScriptHelper::GetTableBool(_LObj, "Looping", false);
	SetFlashClip(FlashMovie);
	SetLoop(Looping);
	return true;
}
bool		UIFlashWidget::ExportSpecificsToFile(script::WriteLua*	pWL)
{
	
// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	if (m_pFlashMovie)
	{
		pWL->WritePair("FlashMovie", m_pFlashMovie->GetFile());
		pWL->WritePair("Looping", m_pFlashMovie->GetLoop() ? "1" : "0");
	}
#endif
	
	return true;
}


bool		UIFlashWidget::Tick(float dt)
{
	
// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	if (!IsActive()) return false;
	UIWidget::Tick(dt);	// tick children, etc
	if (m_pFlashMovie)
		m_pFlashMovie->Tick(dt);
#endif
	
	
	
	return true;
}
void		UIFlashWidget::ResetResources()
{
	
// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	if (m_pFlashMovie)
	{
		std::string	File = m_pFlashMovie->GetFile();
		UTIL_SAFE_DELETE(m_pFlashMovie);
		SetFlashClip(File);
	}
#endif
	
	UIWidget::ResetResources();
}

void		UIFlashWidget::SetFlashClip(const std::string& File)
{
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	if (m_pFlashMovie && m_pFlashMovie->GetFile() == File) 
	{
		_LOG(MSG_WARNING, "Trying to re-set the file. Reset instead?");
		Reset();
		return;
	}
	UTIL_SAFE_DELETE(m_pFlashMovie);
	if (File.empty()) return;	// empty file
	m_pFlashMovie = new flash::FlashMovie();
	m_pFlashMovie->Load(File);
#endif
}

void		UIFlashWidget::Reset()
{
	
// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	if (m_pFlashMovie)m_pFlashMovie->Reset();
#endif
	
	
}

void		UIFlashWidget::SetLoop(bool b)
{
	
// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	if (m_pFlashMovie)m_pFlashMovie->SetLoop(b);
#endif
	
	
}


bool		UIFlashWidget::IsLooping() const
{

// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	if (m_pFlashMovie) return m_pFlashMovie->GetLoop();
#endif
	
	return false;
}

bool		UIFlashWidget::IsPaused() const
{
// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	if (m_pFlashMovie) return m_pFlashMovie->GetPause();
#endif
	
	
	return false;
}


void		UIFlashWidget::PauseClip(bool b)
{

// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	if (m_pFlashMovie) m_pFlashMovie->SetPause(b);
#endif
	
	
}


bool		UIFlashWidget::Render(bool Force)
{

// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	if (!Force)
	{
		// look for things that could disqualify this one from being rendered
		if (!m_IsVisible)
			return false;	// disable entire widget
		if (m_pManager->GetDragTarget() == this)
			return false;	// will get rendered on top anyways
	}

	if (m_pFlashMovie)
		m_pFlashMovie->Render(GetPosition(), GetSize());
	if (m_pManager) m_pManager->WidgetRendered();

	for (ElementList::iterator It = m_Set.begin(); It != m_Set.end(); It++)
		It->second->Render();
#endif
	

	return true;
}

UIWidget*	UIFlashWidget::GetClone()
{
	
// I don't need this in other platforms yet
#if defined(_PLATFORM_ANDROID) || defined(_PLATFORM_IOS)
#else
	UIFlashWidget*	pNew = new UIFlashWidget(m_pParent, GetCloneName(m_Name));
	FillCloneInfo((UIWidget*)pNew);
	return pNew;
#endif
	
	return NULL;
}


/// register with LUA Bind
void	UIFlashWidget::Register(LuaPlus::LuaState* _pScript)
{
#ifdef _ENABLE_LUABIND
    lua_State * L = _pScript->GetCState ( );

	luabind::module(L)
    [

		luabind::class_<UIFlashWidget, UIWidget >( "SliderWidget" )
		.def( "SetFlashClip",		&UIFlashWidget::SetFlashClip)
		.def( "Reset",				&UIFlashWidget::Reset)
		.def( "SetLoop",			&UIFlashWidget::SetLoop)
		.def( "IsLooping",			&UIFlashWidget::IsLooping)
		.def( "IsPaused",			&UIFlashWidget::IsPaused)
		.def( "PauseClip",			&UIFlashWidget::PauseClip)
	];

#endif // _ENABLE_LUABIND    

}
